package common;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import java.io.IOException;

public class FastDFSClientUtil {

	private TrackerClient trackerClient = null;
	private TrackerServer trackerServer = null;
	private StorageServer storageServer = null;
	private StorageClient1 storageClient = null;
	public static final String FDFS_URL="http://192.168.52.128:8888/";

	static {
//根据FastDFSClientUtil类被引用的工程动态获取工程里的resource里的fdfs_client.conf配置文件。所以要将配置文件放到引用此类的工程里
		String configPath=FastDFSClientUtil.class.getResource("/").getPath()+"fdfs_client.conf";
		System.out.println("configPath = " + configPath);
		try {
			ClientGlobal.init(configPath);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
	//核心的四个类  可以用无参构造器，也可以用有参构造器
	public FastDFSClientUtil() throws Exception {
		trackerClient = new TrackerClient();
		trackerServer = trackerClient.getConnection();
		storageServer = null;
		storageClient = new StorageClient1(trackerServer, storageServer);
	}

//	public FastDFSClientUtil(String conf) throws Exception {
//		if (conf.contains("classpath:")) {
//			conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
//		}
//		ClientGlobal.init(conf);
//		trackerClient = new TrackerClient();
//		trackerServer = trackerClient.getConnection();
//		storageServer = null;
//		storageClient = new StorageClient1(trackerServer, storageServer);
//	}


	/**
	 * @param fileName 文件全路径
	 * @param extName 文件扩展名，不包含（.）
	 * @param metas 文件扩展信息
	 */
	public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
		String result = storageClient.upload_file1(fileName, extName, metas);
		return result;
	}

	public String uploadFile(String fileName) throws Exception {
		return uploadFile(fileName, null, null);
	}

	public String uploadFile(String fileName, String extName) throws Exception {
		return uploadFile(fileName, extName, null);
	}


	//@param fileContent 文件的内容，字节数组,将来是穿的是字节而不是路径

	public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
		
		String result = storageClient.upload_file1(fileContent, extName, metas);
		return result;
	}
	
	public String uploadFile(byte[] fileContent) throws Exception {
		return uploadFile(fileContent, null, null);
	}
	
	public String uploadFile(byte[] fileContent, String extName) throws Exception {
		return uploadFile(fileContent, extName, null);
	}

	public static void main(String[] args) throws Exception {
		//客户端配置文件
		 String conf_filename = "fdfs_client.conf";
		//本地文件，要上传的文件
		 String local_filename = "C:/Users/123123/Desktop/pics/123.jpg";
		FastDFSClientUtil fastDFSClientUtil=new FastDFSClientUtil();
		String path = fastDFSClientUtil.uploadFile(local_filename, "jpg");
		System.out.println("path = " + path);
	}
}
